package network;

import link.*;

/**
 * 
 * @author RogerTroutman
 */
public class Router {

	PhysicalLayer pl;

	public Router(PhysicalLayer pl) {
		this.pl = pl;
	}

	public synchronized String forgeNXTHOP(String dest, String payload) {
		if (NetworkLayer.bellman.via.containsKey(dest)) {
			String nextHop = NetworkLayer.bellman.via.get(dest).getNextHop();
			if (nextHop != null) {
				String result = nextHop + ":" + NetworkLayer.myID + ":NXTHOP:"
						+ payload;
				return result;
			}
			return "";
		}
		return "";
	}

	public void processNXTHOP(String msg) {
		String[] splitMessage = msg.split(":");
		String finaldest = splitMessage[3];
		String payload = finaldest;
		for (int i = 4; i < splitMessage.length; i++) {
			payload = payload + ":" + splitMessage[i];
		}
		if (finaldest.equals(NetworkLayer.myID)) {
			NetworkLayer.handler.handleMessage(payload);
		} else {
			NetworkLayer.send(payload);
		}
	}

}
